import xlwings as xw
import time
#
# print('程序开始运行，请稍等...')
# start = time.time()
# # 第一种写法
# # wb = xw.Book()
# # sht = wb.sheets[0]
# # 第二种写法
# app = xw.App()
# wb = app.books.add()
# sht = wb.sheets.add()
#
# info_list = [['110202111111234', '帐篷', '5'],
#              ['110202111118891', '行李箱', '16'],
#              ['110202111111004', '微波炉', '20'],
#              ['110202111132741', '电冰箱', '13'],
#              ['110202111109852', '乐事薯片', '30'],
#              ['110202111112030', '鲁花花生油', '12'],
#              ['110202111190391', '羽绒服', '9'],
#              ['110202111122319', '防晒霜', '18'],
#              ]
# # 写入数据
# titles = ['商品编号', '名称', '数量']
# sht.range('a1').value = titles
# sht.range('a2').value = info_list
# # 保存数据
# wb.save('goods.xlsx')
# # 读取数据
# goods_lsit = sht.range('A2').expand('table').value
# # 读取后进行拆分
# for goods in goods_lsit:
#     # print(goods)
#     goods[0] = str(int(goods[0]))
#     goods[2] = int(goods[2])
# # 创建新的数据集
# new_info = [['110202111111234', '帐篷', 5],
#             ['110202111118891', '行李箱', 16],
#             ['110202111111004', '微波炉', 20],
#             ['110202111132741', '电冰箱', 10],
#             ['110202111124660', '羊毛衫', 8],
#             ['110202111109852', '乐事薯片', 10],
#             ['110202111112030', '鲁花花生油', 12],
#             ['110202111190391', '羽绒服', 0],
#             ['110202111122319', '防晒霜', 9],
#             ['110202111124560', '牛仔裤', 18],
#             ['110202111134798', '老爹鞋', 11]]
# # 去重
# extra = [i for i in new_info if i not in goods_lsit]
#
# ids = sht.range('A2:B1').expand(mode = 'down').value
# print(ids)
# ids = [str(int(id) for id in ids)]
# rows = len(sht.range('a2').expand(mode = 'table').value)
# # 更新已有数据的库存
# for goods in extra:
#     if goods[0] in ids:
#         row_nuber = ids.index(goods[0])
#         print(row_nuber, goods[1])
#         sht[row_nuber + 1, 2].value = goods[2]
#     else:
#         for i in range(3):
#             sht[rows + 1, i].value = goods[i]
#         rows += 1
#
# # sht.clear()
# wb.save('商品信息jiami.xlsx',password='000')
# wb.close()
# app.kill()
# print(fr'运行结束，请查看文件,程序运行时间为{round(time.time() - start, 2)}秒')
import xlwings as xw

# 1、打开Excel程序，处理过程可观
with xw.App(visible=True) as app:
    # 2、选择使用的工作簿
    book = app.books[0]
    # 3、选择使用的工作表
    sheet = book.sheets[0]
    # 4、选择单元格范围，设置背景颜色
    sheet.range('A1').color = '#ff0000'
    # 向单元格写入数据
    sheet.range('A2').value = 'hello Excel'
    # 5、保存工作簿
    book.save('myExcel.xlsx')
